var allScripts = document.getElementsByTagName("script");
var currScript = allScripts[allScripts.length - 1];
var currSrc = currScript.src || "";
var srcHasProtocol = currSrc.indexOf("://") > 0;
var protocol = "";

if (srcHasProtocol) {
	protocol = currSrc.indexOf("https://") == 0 ? "https:" : "http:"
} else {
	protocol = document.location.protocol != "https:" ? "http:" : "https:"
}

var leixpu_waitTime = 5000; //wait time in millis before fetching the piggybacked pixels
var leixpu_server = "cdn.atomex.net", leixpu_secureserver = "cdn.atomex.net";
var leixpu_secure = protocol == "https:" ? true : false;
var komlicdn = protocol + "//" + (leixpu_secure ? leixpu_server : leixpu_secureserver);
var pxsBase = "http://pxs.atomex.net";
var pxsPath = "/pxs/" + leixpu_pixId + "/" + leixpu_hash + "/script.js";
var pxsHtmlPath = "/pxs/" + leixpu_pixId + "/" + leixpu_hash + "/form.html";
var leixpu_pg = document.URL;
var leixpu_ref = document.referrer;
var pxsUrl = pxsBase + pxsPath;
var pxsHtmlUrl = pxsBase + pxsHtmlPath;
var pxsParams = "?page=" + encodeURIComponent(leixpu_pg) + "&referrer=" + encodeURIComponent(leixpu_ref);

function doTheWait(n, callback){
	var handler = callback;
	if ( ("leixpu_waitOver" in window) || n<=0) handler();
	else{
		setTimeout(function(){
			n = n - 1000;
			doTheWait(n, handler);
		}, 1000);
	}
}

function enable_safari_cookies(post_url, paramstr, id){
	var name = 'safari_cookie', iframe = document.createElement('iframe'), form = document.createElement('form');

	iframe.name = name;
	iframe.src = 'javascript:false';
	iframe.style.display = 'block';
	iframe.scrolling = "no";
	iframe.marginHeight = "0";
	iframe.marginWidth = "0";
	iframe.frameBorder = "0";
	iframe.height = "0";
	iframe.width = "0";
	var p = document.getElementById(id);
	p.appendChild(iframe);

	form.action = post_url;
	form.method = 'POST';
	form.target = name;
	var params = paramstr.split('&');
	for(i=0;i<params.length;i++) {
		var parts = params[i].split("=");
		var el = document.createElement("input");
		el.type = "hidden";
		el.name = parts[0];
		el.value = parts[1];
		if(el.name != 'type') {
			form.appendChild(el);
		}
		if(el.name == 'width') {
			iframe.width = el.value;
		} else if(el.name == 'height') {
			iframe.height = el.value;
		}
	}
	p.appendChild(form);

	form.submit();
}

function upixel_function_call_url() {
	var agt=navigator.userAgent.toLowerCase();
	if (agt.indexOf("safari") != -1 && agt.indexOf("chrome") == -1) {
		var id = "pxs_" + Math.random();
		document.write("<div style='margin:0px;padding:0px;border:none;display:inline;float:left;' id='" + id + "'></div>");
		if (window.addEventListener) {
			window.addEventListener('load', function(){enable_safari_cookies(pxsHtmlUrl, pxsParams, id);}, false);
		}
    	} else {
		var url = pxsUrl + pxsParams;
		document.writeln("<script type='text\/javascript' src='" + url + "'><\/script>");
		doTheWait(leixpu_waitTime, function(){
				var iframeObj = document.createElement("iframe");
				iframeObj.height = "0";
				iframeObj.width = "0";
				iframeObj.src = komlicdn + "/amt/" + leixpu_pixId + "/pixel.html";
				document.getElementsByTagName("body")[0].appendChild(iframeObj);
			});
	}
}

var main = new function() {
	if (typeof (leixpu_pixId) == "undefined" || typeof (leixpu_hash) == "undefined") {
        	return
	} else {
        	upixel_function_call_url(leixpu_pixId, leixpu_hash)
	}
};
